<?php
/**
 * User: bgl
 * Date: 2023/5/8
 * Time: 16:19
 */

namespace App\Importer\ProductStockup;

use App\Exporter\Util\ArrExporter;
use App\Importer\Basics\BImporter;
use App\Models\Product\ProductSku;
use App\Models\ProductStockup\StockupSkuPackingSpec;
use App\Util\RExcel;
use Encore\Admin\Actions\Action;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Storage;
use Maatwebsite\Excel\Facades\Excel;

class ImportSkuPackingSpec extends Action{

    public $name = '导入sku规格';
    protected $selector = '.import';

    public function handle(Request $request){

        $file = $request->file('source_file');
        if (!$file->isValid()) return $this->response()->error('无效文件！')->topRight();

        $errors[1] = '重复记录';

        $res[] = ['行','出错信息'];
        foreach($errors as $k => $v){
            if(!$v)continue;
            $res[] = [$k,$v];
        }

        Excel::download(new ArrExporter($res),'上传结果.csv');
        RExcel::importResult($res);
        return back()->withInput();

        echo "<pre>";
        print_r(111);
        echo "</pre>";
        exit;
        try{
            $data = Excel::toArray(new BImporter(), $file);
            $data = $data[0];
            if(!$data)return ['flag' => false,'data' => '请上传包含有数据的文件！'];

            $errors = [];
            $count = 1;
            $skuArr = [];

            DB::beginTransaction();
            foreach($data as $row){
                $count++;
                $sku = $row[0] ? trim($row[0]) : '';
                $numPerBox = is_null($row[1]) ? null : trim($row[1]);
                if(!$sku){
                    $errors[$count] = '必填项为空';
                    continue;
                }
                if(!in_array($sku,$skuArr)){
                    $skuArr[$count] = $sku;
                }else{
                    $errors[$count] = '重复记录';
                    continue;
                }
                $pSkuId = ProductSku::getValueByCode($sku,'id');
                if(!$pSkuId){
                    $errors[$count] = $sku.'系统不存在';
                    continue;
                }
                $skuPackingSpec = StockupSkuPackingSpec::query()->firstOrNew(['psku_id' => $pSkuId]);
                if(!is_null($numPerBox)){
                    $skuPackingSpec->num_per_box = $numPerBox;
                }
                $skuPackingSpec->save();
            }
            DB::commit();

            if(empty($errors)){
                return $this->response()->success('导入完成！')->refresh()->topRight();
            }else{

                $res[] = ['行','出错信息'];
                foreach($errors as $k => $v){
                    if(!$v)continue;
                    $res[] = [$k,$v];
                }

                $data = StockupSkuPackingSpec::query()->get()->toArray();

                return RExcel::importResult($errors);
                //echo "<pre>";
                //print_r($res);
                //echo "</pre>";
                //exit;

               // return $this->response()->error('系统异常！')->download(RExcel::importResult($errors));
              //return RExcel::importResult($errors);
            }

        }catch(\Exception $e){
            DB::rollBack();
            report($e);
            return $this->response()->error('系统异常！')->topRight();
        }

    }

    public function form(){
        $this->file('source_file', '请选择文件');
    }

    public function html(){
        return <<<HTML
        <a class="btn btn-sm btn-default import"><i class="fa fa-upload"></i>导入数据</a>
HTML;
    }

    public function template(Request $request){
        $temp = $request->get('temp');
        $suffix = $request->get('suffix','xlsx');
        return Storage::disk('admin')->download("template/{$temp}-template.{$suffix}");
    }

}
